package example2;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class FruitTest {
    /**
     * 读取(config.properties)配置文件，获取水果类名，并用反射创建水果对象
     * 创建榨汁机对象，并调用run方法
     */
    public static void main(String[] args) {
        Properties properties = new Properties();
        try (FileInputStream fileInputStream = new FileInputStream("Reflection\\src\\config.properties")) {
            properties.load(fileInputStream);
            // 读取配置文件，获取水果类名
            String fruitName = properties.getProperty("fruitName");
            Class<?> aClass = Class.forName(fruitName);
            Constructor<?> constructor = aClass.getConstructor();
            // 用反射创建水果对象
            Object object = constructor.newInstance();
            Juicer juicer = new Juicer();
            juicer.run((Fruit) object);

        } catch (IOException | ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
                 InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }
}
